package com.bw.home

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.bw.common.utils.ImmersiveNightModeUtil
import com.bw.home.adapter.HomeListAdapter
import com.bw.home.databinding.FragmentHomeBinding
import com.bw.home.model.HomeListEntity
import com.bw.home.model.HomeListEntityItem
import com.bw.home.viewmodel.HomeViewModel
import com.bw.lib_base.base.BaseFragment
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import android.app.AlertDialog
import android.widget.ImageView
import com.blankj.utilcode.util.ToastUtils

class HomeFragment : BaseFragment<FragmentHomeBinding,HomeViewModel>() {
    var list:HomeListEntity = HomeListEntity()
    private lateinit var adapter: HomeListAdapter
    
    override fun setupUI() {
        // 初始化时应用保存的主题设置
        ImmersiveNightModeUtil.applySavedNightMode()
    }

    override fun fetchData() {
        vm.getHome()
        binding.rv.layoutManager = LinearLayoutManager(context)
        adapter = HomeListAdapter(list)
        binding.rv.adapter = adapter
    }

    override fun observeData() {
        lifecycleScope.launch {
            vm.homeData.collect{
                Log.d("===","observeData:${it.toString()}")
                if(it!=null){
                    list.clear()
                    list.addAll(it)
                    adapter.notifyDataSetChanged()
                    Log.d("===","observeData:${list.toString()}")
                }
            }
        }
    }

    override fun handleEvent() {
        binding.type.setOnClickListener {
            showThemeDialog()
        }
    }
    
    private fun showThemeDialog() {
        val currentMode = if (ImmersiveNightModeUtil.isNightModeEnabled()) "夜间模式" else "白天模式"
        val items = arrayOf("夜间模式", "白天模式", "跟随系统", "沉浸式状态栏")
        val checkedItem = when {
            ImmersiveNightModeUtil.isNightModeEnabled() -> 0
            else -> 1
        }
        
        AlertDialog.Builder(requireContext())
            .setTitle("主题设置")
            .setSingleChoiceItems(items, checkedItem) { dialog, which ->
                when (which) {
                    0 -> {
                        ImmersiveNightModeUtil.setNightMode(true)
                        ToastUtils.showShort("已切换到夜间模式")
                    }
                    1 -> {
                        ImmersiveNightModeUtil.setNightMode(false)
                        ToastUtils.showShort("已切换到白天模式")
                    }
                    2 -> {
                        ImmersiveNightModeUtil.followSystemNightMode()
                        ToastUtils.showShort("已设置为跟随系统")
                    }
                    3 -> {
                        ImmersiveNightModeUtil.setImmersiveStatusBarWithAutoHide(requireActivity())
                        ToastUtils.showShort("已开启沉浸式状态栏")
                    }
                }
                dialog.dismiss()
            }
            .setNegativeButton("取消") { dialog, _ ->
                dialog.dismiss()
            }
            .show()
    }

    override val mLayoutResId: Int
        get() = R.layout.fragment_home

    override fun lazyLoad() {
        // 延迟加载时也应用主题设置
        ImmersiveNightModeUtil.applySavedNightMode()
    }
}